import React, { Component } from 'react';
import './CardTable.less';
import classnames from 'classnames';
import dataFormat from '../../../utils/dataFormat';
import { setCardTable } from '../../../utils/setComponentUsed'
import _ from 'lodash-es'
class CardTable extends Component {
    static comName = 'CardTable';

    constructor(props) {
        super(props);
        setCardTable(props.ncProps, props.areaId);
    }

    render() {
        let buttonGroup = null;

        let { invisibleTables, children, title, ncProps, areaId, onAfterEvent, totalItemCode, ...cardTableProps } = this.props;

        React.Children.forEach(children, (child) => {
            if (typeof child.type == 'function') {
                switch (child.type.comName) {
                    case 'ButtonGroup':
                        {
                            let buttonsClickFn = child.props.buttonEvent || (() => { return {} });
                            let buttonsClickObj = buttonsClickFn();
                            if (!buttonsClickObj[areaId + '_Add'] || !buttonsClickObj[areaId + '_Add'].actionCode) {
                                buttonsClickObj[areaId + '_Add'] = { ...buttonsClickObj[areaId + '_Add'], actionCode: 'BodyAdd' };
                            }
                            buttonsClickFn = function () {
                                return buttonsClickObj
                            }

                            buttonGroup = React.cloneElement(child, {
                                ncProps,
                                ctrlAreaId: areaId,
                                buttonEvent: buttonsClickFn
                            });
                        }
                        break;
                    case 'ButtonGroupListCard':
                        {
                            let buttonsClickFn = child.props.buttonEvent || (() => { return {} });
                            let buttonsClickObj = buttonsClickFn();
                            if (!buttonsClickObj[areaId + '_Add'] || !buttonsClickObj[areaId + '_Add'].actionCode) {
                                buttonsClickObj[areaId + '_Add'] = { ...buttonsClickObj[areaId + '_Add'], actionCode: 'BodyAdd' };
                            }
                            buttonsClickFn = function () {
                                return buttonsClickObj
                            }

                            buttonGroup = React.cloneElement(child, {
                                ncProps,
                                ctrlAreaId: areaId,
                                buttonEvent: buttonsClickFn
                            });
                        }
                        break;
                    default:
                        console.warn('CardTable标签仅渲染ButtonGroup标签')
                }
            }

        });

        window.__ssc_BillStyleVar.tablesCount[areaId] || (window.__ssc_BillStyleVar.tablesCount[areaId] = {
            rowCount: 0,
            totalItemCount: 0,
            totalItemCode: totalItemCode
        })
        let tableCount = window.__ssc_BillStyleVar.tablesCount[areaId];
        let { rowCount, totalItemCount } = tableCount;



        this.getTableHead = () => {
            return (
                [buttonGroup]
            );
        };

        this.getTableLeftHead = () => {

            const rows = ncProps.cardTable.getVisibleRows(areaId);
            let sumJsx = null;

            if (totalItemCode) {
                let total = 0

                let scale = _.get(rows[0],`values.${totalItemCode}.scale`, 2)
                if (scale < 0 || isNaN(scale)) {
                    scale = 2
                }
                rows.forEach(item => {
                    if (item.values&&item.values[totalItemCode]&&!isNaN(Number(item.values[totalItemCode].value))) {
                        let rowValue = parseFloat(item.values[totalItemCode].value) * Math.pow(10, scale); //汇总
                        if (!isNaN(rowValue)) {
                            total += rowValue
                        }
                    }
                })
                total = isNaN(total) ? 0 : (total / Math.pow(10, scale)).toFixed(scale);
                sumJsx = [
                    '，',
                    this.props.multiLang['105601GGZD-0064'] || '', //合计
                    ' ',
                    <span style={{ color: '#111', fontWeight: 'bold' }} className="nc-theme-common-font-c">{dataFormat(total, 'number')}</span>
                ]
            }
            return (
                <span ref={(obj) => { tableCount.totalBarDom = obj; }} className="nc-theme-common-font-c" style={{ color: '#555', fontSize: '14px', lineHeight: '40px' }}>
                    {/*  共 */}
                    {(this.props.multiLang['105601GGZD-0062'] || '')}
                    <span style={{ color: '#111', fontWeight: 'bold' }} className="nc-theme-common-font-c">
                        {' ' + rows.length + ' '}
                    </span>
                    {/* 条 */}
                    {this.props.multiLang['105601GGZD-0063'] || ''}
                    {sumJsx}
                </span>
            )
        }

        const meta = ncProps.meta.getMeta()
        const isVisible = _.get(meta, `${areaId}.areaVisible`, false)

        return (
            <div className={classnames("nc-bill-tableTab-area", { "invisible-card-table": !isVisible || invisibleTables && invisibleTables.includes(areaId) })}>
                {ncProps.cardTable.createCardTable(areaId, {
                    tableHead: this.getTableHead,
                    tableHeadLeft: this.props.showTotal === false ? null : this.getTableLeftHead,
                    modelSave: null,//
                    showIndex: true,
                    isAddRow: false,
                    ...cardTableProps,
                    onAfterEvent: (props, moduleId, key, value, changedrows) => {

                        if (totalItemCode && totalItemCode == key) {
                            window.__ssc_BillStyleVar.tableRowAndItemCount(areaId, 'rowChange', changedrows);
                            //window.__ssc_BillStyleVar.tableRowAndItemCount(areaId, ncProps.cardTable);
                        }
                        onAfterEvent && onAfterEvent(props, moduleId, key, value, changedrows);
                    }
                })}
            </div>
        )
    }
}

export default CardTable
